package com.Cfeng.XiaohuanChat.handler;

import com.Cfeng.XiaohuanChat.domain.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Cfeng
 * @date 2022/8/13
 * 服务层异常都是throws到controller层进行处理，在处理器中直接try catch 影响可读性，直接进行全局的异常处理
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public Resp<String> errorHandler(Exception e) {
        log.error("全局异常处理>>> 错误信息: {}", e.getMessage());
        return Resp.failed("出错了,错误信息为" + e.getMessage());
    }

    @ExceptionHandler(value = {SessionAuthenticationException.class})
    public Resp<String> sessionErrorHandler(SessionAuthenticationException e) {
        log.error("全局异常处理>>> 错误信息: {}", e.getMessage());
        return Resp.failed(e.getMessage());
    }
}
